﻿using UnityEngine;
using System.Collections;
using LitJson;

public class GameUtilities
{
    /// <summary>
    /// 获取两点之间的角度
    /// </summary>
    /// <param name="from"></param>
    /// <param name="to"></param>
    /// <returns></returns>
    public static float GetAngle(Vector3 from, Vector3 to)
    {
        Vector3 nVector = Vector3.zero;
        nVector.x = to.x;
        nVector.y = from.y;
        float a = to.y - nVector.y;
        float b = nVector.x - from.x;
        float tan = a / b;
        return (Mathf.Atan(tan) * 180f)/ Mathf.PI;
    }

    #region json数据转化
    public static JsonData ParseJsonDataFromJsonString(JsonData json, string key)
    {
        if (json == null || string.IsNullOrEmpty(key))
            return null;
        return json[key] as JsonData;
    }

    public static string ParseStringFromJsonData(JsonData json, string key)
    {
        if (json == null || string.IsNullOrEmpty(key))
            return string.Empty;
        return json[key].ToString();
    }

    public static int ParseIntFromJsonData(JsonData json, string key)
    {
        if (json == null || string.IsNullOrEmpty(key))
            return 0;
        string res = ParseStringFromJsonData(json, key);
        if (string.IsNullOrEmpty(res))
            return 0;
        return int.Parse(res);
    }

    public static float ParseFloatFromJsonData(JsonData json, string key)
    {
        if (json == null || string.IsNullOrEmpty(key))
            return 0;
        string res = ParseStringFromJsonData(json, key);
        if (string.IsNullOrEmpty(res))
            return 0;
        return float.Parse(res);
    }

    public static bool ParseBoolFromJsonData(JsonData json, string key)
    {
        if (json == null || string.IsNullOrEmpty(key))
            return false;
        string res = ParseStringFromJsonData(json, key);
        if (string.IsNullOrEmpty(res))
            return false;
        return res == "0" || res == "true" || res == "0.0";
    }
    #endregion
}
